<?php
/**
 * @author kevin
 * @date 2016-03-13 14:00:00
 * @desc 地址管理
 */

if (!defined('IN_ECS'))
{
    die('Hacking attempt');
}
$version = '1.0';   //版本号

if ($_POST['api_version'] != $version)      //网店的接口版本低
{
	api_err('0x008', 'a low version api');
}
if(empty($_POST['st'])){
	api_err('0x008', 'param st is Missing');
}
include_once(ROOT_PATH . 'includes/lib_transaction.php');
//用户ID检测
if(!isset($GLOBALS['uid'])){
	api_err('0x015', 'You have not logged on');
}
$user_id = $GLOBALS['uid'];
//地址列表
if($_POST['st'] == 'list'){
	$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
	$size = abs(intval($_POST['size']));
	$size = ($size > 0 && $size < SIZE_MAX)?$size:SIZE_VALUE;
	$count = $GLOBALS['db']->getOne("SELECT COUNT(*) FROM " .$ecs->table('user_address').
                                " WHERE user_id='$user_id' ORDER BY address_id DESC");
	$pager = get_pager('user.php', array('act' => 'collection_list'), $count, $page, $size);
	$data['list'] = get_consignee_list_v1($user_id, $page, $size);
	$data['page'] = get_app_pager($pager);
	$address_id  = $GLOBALS['db']->getOne("SELECT address_id FROM " .$ecs->table('users'). " WHERE user_id='$user_id'");
	//取得国家列表，如果有收货人列表，取得省市区列表
    foreach ($data['list'] AS $region_id => $consignee)
    {
        $consignee['country']  = isset($consignee['country'])  ? intval($consignee['country'])  : 0;
        $consignee['province'] = isset($consignee['province']) ? intval($consignee['province']) : 0;
        $consignee['city']     = isset($consignee['city'])     ? intval($consignee['city'])     : 0;
		$addressAll = '';
		$names = get_current_name(0, $consignee['country']);
		$addressAll .= $names['region_name'];
		$names = get_current_name(1, $consignee['province']);
		$addressAll .= $names['region_name'];
		$names = get_current_name(2, $consignee['city']);
		$addressAll .= $names['region_name'];
		$names = get_current_name(3, $consignee['district']);
		$addressAll .= $names['region_name'];
		$data['list'][$region_id]['address_all'] = $addressAll.$consignee['address'];
		$data['list'][$region_id]['is_default'] = 0;
		if($consignee['address_id'] == $address_id){
			$data['list'][$region_id]['is_default'] = 1;
		}
    }
}
//添加/更新地址
if($_POST['st'] == 'update'){
	$country = 1;//isset($_POST['country'])   ? intval($_POST['country'])  : 0;
	$province = isset($_POST['province'])  ? intval($_POST['province']) : 0;
	if(empty($province)){
		api_err('0x008', 'province can not empty');
	}
	$city = isset($_POST['city'])      ? intval($_POST['city'])     : 0;
	if(empty($city)){
		api_err('0x008', 'city can not empty');
	}
	$district = isset($_POST['district'])  ? intval($_POST['district']) : 0;
	if(empty($district)){
		api_err('0x008', 'district can not empty');
	}
	$address = isset($_POST['address'])   ? compile_str(trim($_POST['address']))    : '';
	if(empty($address)){
		api_err('0x008', 'address can not empty');
	}
	$consignee = isset($_POST['consignee']) ? compile_str(trim($_POST['consignee']))  : '';
	if(empty($consignee)){
		api_err('0x008', 'consignee can not empty');
	}
	$tel = isset($_POST['tel']) ? compile_str(trim($_POST['tel']))  : '';
	if(empty($tel)){
		api_err('0x008', 'tel can not empty');
	}
	$address = array(
        'user_id'    => $user_id,
		'address_id' => intval($_POST['address_id']),
        'country'    => $country,
        'province'   => $province,
        'city'       => $city,
        'district'   => $district,
        'address'    => $address,
        'consignee'  => $consignee,
        'email'      => isset($_POST['email'])     ? compile_str(trim($_POST['email']))      : '',
        'tel'        => $tel,
        'mobile'     => isset($_POST['mobile'])    ? compile_str(make_semiangle(trim($_POST['mobile']))) : '',
        'best_time'  => isset($_POST['best_time']) ? compile_str(trim($_POST['best_time']))  : '',
        'sign_building' => isset($_POST['sign_building']) ? compile_str(trim($_POST['sign_building'])) : '',
        'zipcode'       => isset($_POST['zipcode'])       ? compile_str(make_semiangle(trim($_POST['zipcode']))) : '',
		'default'       =>isset($_POST['default'])   ? intval($_POST['default'])  : 0
    );
    if(!update_address($address)){
        api_err('0x008', 'address saved fail');
    }
	$data = array();
}
//删除地址
if($_POST['st'] == 'delete'){
	$address_id = intval($_POST['address_id']);
	if(empty($address_id)){
		api_err('0x008', 'param address_id can not empty');
	}
    if(!drop_consignee_v1($address_id,$user_id)){
        api_err('0x008', 'address delete fail');
    }
	$data = array();
}
//获取地址详情
if($_POST['st'] == 'detail'){	
	$address_id = intval($_POST['address_id']);
	if(empty($address_id)){
		api_err('0x008', 'param address_id can not empty');
	}
	$default_address_id  = $GLOBALS['db']->getOne("SELECT address_id FROM " .$ecs->table('users'). " WHERE user_id='$user_id'");
	$data = get_address_info($address_id, $user_id);
    $data['is_default'] = ($default_address_id == $data['address_id'])?1:0;
}
//设置默认地址
if($_POST['st'] == 'set_default'){
	$address_id = intval($_POST['address_id']);
	if(empty($address_id)){
		api_err('0x008', 'param address_id can not empty');
	}
	$sql = "update ".$ecs->table('users')." set address_id = '{$address_id}'  WHERE user_id='$user_id'";
	if(!$GLOBALS['db'] ->query($sql)){
		api_err('0x008', 'setting default address fail');
	}
	$data = array();
}
//获取省列表
if($_POST['st'] == 'province'){
	$data = get_regions(1, 1);
}
//获取市列表
if($_POST['st'] == 'city'){
	$region_id = intval($_POST['region_id']);
	if(empty($region_id)){
		api_err('0x008', 'param region_id can not empty');
	}
	$data = get_regions(2, $region_id);
}
//获取地区列表
if($_POST['st'] == 'district'){
	$region_id = intval($_POST['region_id']);
	if(empty($region_id)){
		api_err('0x008', 'param region_id can not empty');
	}
	$data = get_regions(3, $region_id);
}
//st参数错误
if(!isset($data)){
	api_err('0x008');
}
data_back($data, '', RETURN_TYPE);  //返回数据

